package com.gzsxy.NIO.netty.Protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 编码器（指定写入msg的字节长度）
 * @date 2022/5/25 20:39
 */
public class MyMessageEncoder extends MessageToByteEncoder<MessageProtocol> {
    @Override
    protected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception {
        System.out.println("MyMessageEncoder encode 方法被调用");
        //指定字节写入长度
        out.writeInt(msg.getLen());
        out.writeBytes(msg.getContent());
    }

}
